package com.tvunetworks.center.user.util;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
 * @program: tvucc-aggregator
 * @description: MemcacheUtil
 * @author lebronchen
 * @create: 2020-04-21 14:28
 **/
@Component
public class RedisUtil {

    @Resource(name="redisTemplate")
    private RedisTemplate<String, Object> redisTemplate;

    /**
     *
     * @param key key
     * @param value value
     */
    public void set(String key, Object value){
        redisTemplate.boundValueOps(key).set(value);
    }

    /**
     *
     * @param key key
     * @param value value
     * @param time time
     */
    public void set(String key, Object value, Long time){
        redisTemplate.boundValueOps(key).set(value, time, TimeUnit.SECONDS);
    }

    /**
     *
     * @param key key
     * @return Object
     */
    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    /**
     *
     * @param key key
     * @param toValueType toValueType
     * @param <T>  t
     * @return t
     */
    public <T> T get(String key, Class<T> toValueType) {
        return (T)redisTemplate.opsForValue().get(key);
    }

    /**
     *
     * @param pattern pattern
     * @return t
     */
    public Set<String> keys(String pattern) {
        return redisTemplate.keys(pattern);
    }

    /**
     *
     * @param key key
     * @return Boolean
     */
    public Boolean delete(String key) {
        return redisTemplate.delete(key);
    }

    /**
     *
     * @param key key
     * @return Object
     */
    public Object lpop(String key) {
        return redisTemplate.opsForList().leftPop(key);
    }

    /**
     *
     * @param key key
     * @param values values
     * @return Long
     */
    public Long rpush(String key, Object... values){
        return redisTemplate.opsForList().rightPushAll(key, values);
    }

    /**
     *
     * @param key key
     * @param k k
     * @return Object
     */
    public Object hashGet(String key, Object k){
        return redisTemplate.opsForHash().get(key, k);
    }

    /**
     *
     * @param key key
     * @return Set
     */
    public Set<Object> hashKeys(String key){
        return redisTemplate.opsForHash().keys(key);
    }

    /**
     *
     * @param key key
     * @return List
     */
    public List<Object> hashValues(String key){
        return redisTemplate.opsForHash().values(key);
    }

    /**
     *
     * @param key key
     * @return Map
     */
    public Map<Object, Object> hashEntry(String key){
        return redisTemplate.opsForHash().entries(key);
    }

    /**
     *
     * @param key key
     * @param k k
     * @return Long
     */
    public Long hashRemove(String key, Object k){
        return redisTemplate.opsForHash().delete(key, k);
    }

    /**
     *
     * @param key key
     * @param k k
     * @param value value
     */
    public void hashPut(String key, Object k, Object value){
        redisTemplate.opsForHash().put(key, k, value);
    }

    /**
     *
     * @param key key
     * @param map map
     */
    public void hashPutAll(String key, Map map){
        redisTemplate.opsForHash().putAll(key, map);
    }

    /**
     *
     * @param key key
     * @param values  values
     * @return Long
     */
    public Long sadd(String key, Object... values){
        return redisTemplate.opsForSet().add(key, values);
    }

    /**
     *
     * @param key key
     * @param values values
     * @return sremove
     */
    public Long sremove(String key, Object... values){
        return redisTemplate.opsForSet().remove(key, values);
    }

    /**
     *
     * @param key key
     * @param values values
     * @return isMember
     */
    public boolean isMember(String key, Object... values){
        Boolean member = redisTemplate.opsForSet().isMember(key, values);
        return member != null && member;
    }

    /**
     *
     * @param key key
     * @return boolean
     */
    public boolean hasKey (String key){
        Boolean aBoolean = redisTemplate.hasKey(key);
        return aBoolean != null && aBoolean;
    }

    /**
     *
     * @param channel channel
     * @param message message
     */
    public void sendMessageToChannel(String channel, Object message){
        redisTemplate.convertAndSend(channel, message);
    }

    /**
     *
     * @param bytes bytes
     * @return Object
     */
    public Object deserialize(byte[] bytes) {
        return redisTemplate.getValueSerializer().deserialize(bytes);
    }
}
